import request from '@/utils/request'

// 模拟数据 - 批次列表
const mockBatches = [
  {
    id: 1,
    batchNo: 'BATCH-2023-001',
    productId: 1,
    productName: '管道阀门A型',
    quantity: 100,
    status: '已完成',
    startDate: '2023-10-01',
    endDate: '2023-10-15',
    createTime: '2023-09-25',
    remark: '常规生产批次'
  },
  {
    id: 2,
    batchNo: 'BATCH-2023-002',
    productId: 2,
    productName: '管道接头B型',
    quantity: 150,
    status: '已完成',
    startDate: '2023-11-01',
    endDate: '2023-11-20',
    createTime: '2023-10-25',
    remark: '特殊规格批次'
  },
  {
    id: 3,
    batchNo: 'BATCH-2023-003',
    productId: 3,
    productName: '管道连接器C型',
    quantity: 80,
    status: '已完成',
    startDate: '2023-12-01',
    endDate: '2023-12-10',
    createTime: '2023-11-25',
    remark: '小批量生产'
  },
  {
    id: 4,
    batchNo: 'BATCH-2024-001',
    productId: 1,
    productName: '管道阀门A型',
    quantity: 120,
    status: '已完成',
    startDate: '2024-01-10',
    endDate: '2024-01-25',
    createTime: '2024-01-05',
    remark: '常规生产批次'
  },
  {
    id: 5,
    batchNo: 'BATCH-2024-002',
    productId: 2,
    productName: '管道接头B型',
    quantity: 200,
    status: '已完成',
    startDate: '2024-02-05',
    endDate: '2024-02-20',
    createTime: '2024-01-30',
    remark: '大批量生产'
  }
];

// 使用模拟数据响应
const useMockData = true;

// 查询批次列表
export function listBatches(query) {
  if (useMockData) {
    console.log('Mock API: listBatches', query);
    
    // 使用模拟数据
    let filteredBatches = [...mockBatches];
    
    // 应用筛选条件
    if (query) {
      if (query.batchNo) {
        filteredBatches = filteredBatches.filter(batch => 
          batch.batchNo.toLowerCase().includes(query.batchNo.toLowerCase()));
      }
      if (query.productId) {
        filteredBatches = filteredBatches.filter(batch => 
          batch.productId === parseInt(query.productId));
      }
      if (query.status) {
        filteredBatches = filteredBatches.filter(batch => 
          batch.status === query.status);
      }
    }
    
    // 处理分页
    const pageSize = query.pageSize || 10;
    const pageNum = query.pageNum || 1;
    const startIndex = (pageNum - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const pagedBatches = filteredBatches.slice(startIndex, endIndex);
    
    return Promise.resolve({
      code: 200,
      msg: 'success',
      total: filteredBatches.length,
      rows: pagedBatches
    });
  }
  
  return request({
    url: '/qep/production/batch/list',
    method: 'get',
    params: query
  })
}

// 兼容原有API名称
export function listBatch(query) {
  return listBatches(query);
}

// 查询批次详细信息
export function getBatch(id) {
  if (useMockData) {
    console.log('Mock API: getBatch', id);
    const batch = mockBatches.find(batch => batch.id === parseInt(id));
    
    return Promise.resolve({
      code: 200,
      msg: 'success',
      data: batch
    });
  }
  
  return request({
    url: '/qep/production/batch/' + id,
    method: 'get'
  })
}

// 查询批次详细信息
export function getBatchInfo(id) {
  return getBatch(id);
}

// 查询产品列表
export function listProduct(query) {
  return request({
    url: '/qep/product/list',
    method: 'get',
    params: query
  })
}

// 新增批次
export function addBatch(data) {
  if (useMockData) {
    console.log('Mock API: addBatch', data);
    // 创建新批次
    const newId = Math.max(...mockBatches.map(batch => batch.id)) + 1;
    const now = new Date().toISOString().split('T')[0];
    const newBatch = {
      ...data,
      id: newId,
      createTime: now
    };
    
    mockBatches.push(newBatch);
    
    return Promise.resolve({
      code: 200,
      msg: 'success',
      data: newBatch
    });
  }
  
  return request({
    url: '/qep/production/batch',
    method: 'post',
    data: data
  })
}

// 修改批次信息
export function updateBatch(data) {
  if (useMockData) {
    console.log('Mock API: updateBatch', data);
    const index = mockBatches.findIndex(batch => batch.id === data.id);
    if (index !== -1) {
      mockBatches[index] = { ...mockBatches[index], ...data };
    }
    
    return Promise.resolve({
      code: 200,
      msg: 'success'
    });
  }
  
  return request({
    url: '/qep/production/batch',
    method: 'put',
    data: data
  })
}

// 删除批次
export function delBatch(id) {
  if (useMockData) {
    console.log('Mock API: delBatch', id);
    const index = mockBatches.findIndex(batch => batch.id === parseInt(id));
    if (index !== -1) {
      mockBatches.splice(index, 1);
    }
    
    return Promise.resolve({
      code: 200,
      msg: 'success'
    });
  }
  
  return request({
    url: '/qep/production/batch/' + id,
    method: 'delete'
  })
}

// 导出批次数据
export function exportBatch(query) {
  if (useMockData) {
    console.log('Mock API: exportBatch', query);
    // 创建一个简单的Excel blob
    const excelContent = 'This is a mock Excel file content';
    const blob = new Blob([excelContent], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    return Promise.resolve(blob);
  }
  
  return request({
    url: '/qep/production/batch/export',
    method: 'get',
    params: query,
    responseType: 'blob'
  })
}

// 批次追溯查询
export function batchTrace(batchCode) {
  if (useMockData) {
    console.log('Mock API: batchTrace', batchCode);
    const batch = mockBatches.find(batch => batch.batchNo === batchCode);
    
    // 模拟追溯数据
    const traceData = {
      batch: batch,
      processes: [
        { name: '原材料检验', startTime: '2023-10-01 08:00', endTime: '2023-10-01 10:00', status: '已完成', operator: '张三' },
        { name: '加工', startTime: '2023-10-02 08:00', endTime: '2023-10-05 17:00', status: '已完成', operator: '李四' },
        { name: '装配', startTime: '2023-10-06 08:00', endTime: '2023-10-10 17:00', status: '已完成', operator: '王五' },
        { name: '质量检验', startTime: '2023-10-11 08:00', endTime: '2023-10-12 17:00', status: '已完成', operator: '赵六' },
        { name: '包装', startTime: '2023-10-13 08:00', endTime: '2023-10-15 12:00', status: '已完成', operator: '孙七' }
      ]
    };
    
    return Promise.resolve({
      code: 200,
      msg: 'success',
      data: traceData
    });
  }
  
  return request({
    url: '/qep/production/batch/trace/' + batchCode,
    method: 'get'
  })
} 